package boot.spring.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;

public class Test {
	
	@Autowired
	static
	TaskService taskservice;
	@Autowired
	static
	HistoryService histiryservice;
	//public static ManagementService managementService = (ManagementService) SpringContextUtils.getBean("managementService", true);


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String taskid = "182572";
		String targetFlowNodeId = "manager";
		//multiGroup/manager
		jump2TargetFlowNode(taskid, targetFlowNodeId);
//		String curTaskId = "157517";
//		Task task = null;
//        Map<Integer, String> pList = new HashMap<>();
// 
//        task = (Task) taskservice.createTaskQuery();
//        		//.taskId(curTaskId);
//        //.singleResult();//使用任务ID查询
// 
//        if (task == null) {
//           // throw new ServiceException("任务不存在");
//            System.out.println("任务不存在");
//        }
//        String processInstanceId = task.getProcessInstanceId();
//      //与历史数据（历史表）相关的service
//      //创建历史任务实例查询
//        List<HistoricTaskInstance> list = histiryservice.createHistoricTaskInstanceQuery()
//        		.processInstanceId(processInstanceId).list();
//        //logger.info("上一节点任务ID：{}" + list.get(list.size() - 2).getId());
//        System.out.println("上一节点任务ID：{}" + list.get(list.size() - 2).getId());
//        List<Comment> commentList = taskservice.getTaskComments(list.get(list.size() - 2).getId());
//        if (commentList.size()>0) {
//        	System.out.println(commentList.get(0).getFullMessage());
//        }else {
//        	System.out.println("");
//        }
        

	}
	/**
	 * 跳转到指定流程节点
	 * 
	 * @param curTaskId
	 * @param targetFlowNodeId
	 *            指定的流程节点ID 比如跳转<endEvent id="endevent1" name="End"></endEvent> ，则targetFlowNodeId为endevent1
	 */
	public static void jump2TargetFlowNode(String curTaskId, String targetFlowNodeId) {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		ManagementService managementService = engine.getManagementService();
		managementService.executeCommand(new Jump2TargetFlowNodeCommand(curTaskId, targetFlowNodeId));
	}
}
